<?php
if (!defined('IN_ADMIN')) {
    exit('Access Denied');
}

$mpurl = 'admin.php?ac=activity';

// 当前时间
$nowTimeStamp = time();
// 组装名称与标题
$date = date("md", time());
$index = substr(time(), 0, 5);
$pre  = 'home-';
$name = 'banner-'.$date.'-'.$index;
$title = 'banner-'.$date;
if ($_GET['op'] == 'addHomeBannerActivity') {
    $name = $pre.$name;
    $title= $pre.$title;
}

$pics = array(
        'default'        =>    '',
        'iPhone3gs'      =>    '',
        'iPhone4'        =>    '',
        'iPhone5s'       =>    '',
        'iPad2'          =>    '',
        'iPad4'          =>    '',
        'And_480_800'    =>    '',
        'And_480_854'    =>    '',
        'And_540_960'    =>    '',
        'And_720_1280'   =>    '',
        'And_800_1280'   =>    '',
);

if ($_GET['op'] == 'addLaunchActivity'){
    $param = restructData($_POST);
    $pics['default'] = $pics['iPhone5s'] = strval($param['image_url']);
    $param['extend']['pics'] = json_encode($pics);
    $param['do'] = 'addActivity';
    $param['type'] = 'launch';
    $param['format'] = 'json';
    
    $result  = requestAndResponse($_SC['apiurl'], $param);
    
    $msg = '添加失败!';
    if ($result == 1) {
        $msg = "添加成功!";
    }
    cpmessage($msg, $mpurl, 1);
}elseif ($_GET['op'] == 'updateReviewBanners'){

    $url = "http://tips.ffrj.net/banner/create";
    $json = getHttpResponsePOST($url);
    $result = parseData($json);
    echo '<pre>';var_dump($result);exit;
    exit();
}elseif($_GET['op'] == 'setLaunchActivity'){
    $param = restructData($_POST);
    $pics['default'] = $pics['iPhone5s'] = strval($param['image_url']);
    $param['extend']['pics'] = json_encode($pics);
    $param['do'] = 'setActivity';
    $param['type'] = 'launch';
    $param['format'] = 'json';

    $result  = requestAndResponse($_SC['apiurl'], $param);
    
    $msg = '修改失败!';
    if ($result == 1) {
        $msg = "修改成功!";
    }
    cpmessage($msg, $mpurl, 1);
}elseif($_GET['op'] == 'addWisdomActivity'){
    
    $param = restructData($_POST);
    $param['title'] = '每日一语';
    $param['do'] = 'addActivity';
    $param['type'] = 'wisdom';
    $param['format'] = 'json';
    $param['content'] = urlencode($param['content']);

    $result  = requestAndResponse($_SC['apiurl'], $param);
    $msg = '添加失败!';
    if ($result == 1) {
        $msg = "添加成功!";
    }
    cpmessage($msg, $mpurl, 1);
}elseif($_GET['op'] == 'setWisdomActivity'){
    $param = restructData($_POST);
    $param['do'] = 'setActivity';
    $param['type'] = 'wisdom';
    $param['format'] = 'json';
    $param['content'] = urlencode($param['content']);

    $result  = requestAndResponse($_SC['apiurl'], $param);

    $msg = '修改失败!';
    if ($result == 1) {
        $msg = "修改成功!";
    }
    cpmessage($msg, $mpurl, 1);
}elseif($_GET['op'] == 'addBannerActivity'){
    $param = restructData($_POST);
    $param['do']       = 'addActivity';
    $param['type']     = 'banner';
    $param['format']   = 'json';
    $param['title']    = $title;
    $param['name']     = $name;
    
    $result  = requestAndResponse($_SC['apiurl'], $param);
    
    $msg = '添加失败!';
    if ($result == 1) {
        $msg = "添加成功!";
    }
    cpmessage($msg, $mpurl, 1);
}elseif($_GET['op'] == 'setBannerActivity'){
    $param = restructData($_POST);
    $param['do'] = 'setActivity';
    $param['type'] = 'banner';
    $param['format'] = 'json';
    $result  = requestAndResponse($_SC['apiurl'], $param);

    $msg = '修改失败!';
    if ($result == 1) {
        $msg = "修改成功!";
    }
    cpmessage($msg, $mpurl, 1);
}elseif($_GET['op'] == 'addHomeBannerActivity'){
    $param = restructData($_POST);
    $param['do']       = 'addActivity';
    $param['type']     = 'home_banner';
    $param['format']   = 'json';
    $param['title']    = $title;
    $param['name']     = $name;

    $result  = requestAndResponse($_SC['apiurl'], $param);
    
    $msg = '添加失败!';
    if ($result == 1) {
        $msg = "添加成功!";
    }
    cpmessage($msg, $mpurl, 1);
}elseif($_GET['op'] == 'setHomeBannerActivity'){
    $param = restructData($_POST);
    $param['do'] = 'setActivity';
    $param['type'] = 'home_banner';
    $param['format'] = 'json';
    $result  = requestAndResponse($_SC['apiurl'], $param);

    $msg = '修改失败!';
    if ($result == 1) {
        $msg = "修改成功!";
    }
    cpmessage($msg, $mpurl, 1);
}elseif ($_GET['op'] == 'updateActivityImage'){

    $url = "http://tips.ffrj.net/tips/create?ymd=".date('Ymd', time());
//     $url = "http://tips.ffrj.net/tips/create?ymd=20151106";
    $json = getHttpResponsePOST($url);
    $result = parseData($json);
    
    $res = 0;
    if (!empty($result['info']['url']) && !empty($result['info']['image_url'])) {
        $res = 1;
    }
    echo $res;
    exit();
}elseif($_GET['op'] == 'delActivity'){
    $param = array(
            'do'     =>    'delActivity',
            'format' =>    'json'
    );
    isset($_GET['id'])  && $param['id']  = intval($_GET['id']);
    
    $result  = requestAndResponse($_SC['apiurl'], $param);
    
    $msg = '删除失败!';
    if ($result == 1) {
        $msg = "删除成功!";
    }
    cpmessage($msg, $mpurl, 1);
}else{
    $tenDaysAgo    = time();
    $oneMonthLater = time();
    $show          = 1;
    
    $param = array(
            'do'     =>    'getActivity',
            'format' =>    'json',
            'type'   =>    'banner',
//             'startTime'  =>    $tenDaysAgo,
            'endTime'    =>    $oneMonthLater
    );
    
    if ($_GET['show']) {
        $show = 0;
        unset($param['endTime']);
    }

    $content = get_api_content($_SC['apiurl'], $param);
    $content = json_decode($content, true);
    $result  = $content['resultData'];
    
    $bannerList = $result['banner'];
    $launchList = $result['launch'];
    $wisdomList = $result['wisdom'];
    $homeBannerList = $result['home_banner'];

}

// 组装数据结构
function restructData($data){
    $param = array();
    isset($data['id'])         && $param['id']           = intval($data['id']);
    isset($data['name'])       && $param['name']       = strval($data['name']);
    isset($data['title'])      && $param['title']      = strval($data['title']);
    isset($data['content'])    && $param['content']    = strval($data['content']);
    isset($data['image_url'])  && $param['image_url']  = strval(trim($data['image_url']));
    isset($data['link_url'])   && $param['link_url']   = strval(urlencode($data['link_url']));
    isset($data['action_url']) && $param['action_url'] = urlencode(trim($data['action_url']));
    isset($data['start'])      && $param['start']      = intval(strtotime($data['start']));
    isset($data['end'])        && $param['end']        = intval(strtotime($data['end']));
    isset($data['sort'])       && $param['sort']       = intval($data['sort']);
    
    if ($data['day']) {
        $param['start'] = intval(strtotime($data['day']));
        $param['end'] = $param['start'] + 86399;
    }
    
    return $param;
}


